package com.atguigu.linkedlist;

/**
 * @author Amos
 * @date 2022/6/4
 */

public class CombineLinkedListsDemo {
    public static void main(String[] args) {
        MyLinkedList myLinkedList1 = new MyLinkedList();
        myLinkedList1.add(new Student(2, "2"));
        myLinkedList1.add(new Student(5, "5"));
        myLinkedList1.add(new Student(9, "9"));
        System.out.println("myLinkedList1的数据为：");
        myLinkedList1.list();

        System.out.println("================================");

        MyLinkedList myLinkedList2 = new MyLinkedList();
        myLinkedList2.add(new Student(3, "3"));
        myLinkedList2.add(new Student(4, "4"));
        myLinkedList2.add(new Student(7, "7"));
        myLinkedList2.add(new Student(9, "9"));
        myLinkedList2.add(new Student(10, "10"));
        myLinkedList2.add(new Student(12, "12"));
        System.out.println("myLinkedList2的数据为：");
        myLinkedList2.list();

        System.out.println("测试combine方法==================");
        MyLinkedList rstList = myLinkedList1.combine(myLinkedList2);
        rstList.list();

    }
}

class MyLinkedList {
    private Student head = new Student(0, "");

    public Student getHead() {
        return head;
    }

    public void add(Student student) {
        Student temp = head;
        while (true) {
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        temp.next = student;
    }

    public void list() {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        Student temp = head.next;
        while (true) {
            if (temp != null) {
                System.out.println(temp);
            }
            temp = temp.next;
            if (temp == null) {
                break;
            }
        }
    }

    // TODO: 该方法待完善
    public MyLinkedList combine(MyLinkedList lst) {
        if (head.next == null) return lst;
        if (lst.getHead().next == null) return this;

        MyLinkedList rstList = new MyLinkedList();

        Student newHead = rstList.getHead();
        Student head1 = this.getHead();
        Student head2 = lst.getHead();

        Student h = newHead.next;
        Student h1 = head1.next;
        Student h2 = head2.next;

        while (true) {
            if (h1.no <= h2.no) {
                h = h1;
                System.out.println(h1);
                h = h.next;
                h1 = h1.next;
                if (h1 == null) {
                    h = h2;
                    break;
                }
            }
            if (h1.no > h2.no) {
                h = h2;
                rstList.add(h2);
                System.out.println(h2);
                h = h.next;
                h2 = h2.next;
                if (h2 == null) {
                    h = h1;
//                    rstList.add(h1);
                    break;
                }
            }
        }

        return rstList;
    }

}

class Student {
    public int no;
    public String name;
    public Student next;

    public Student(int no, String name) {
        this.no = no;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }
}
